Skip to content

fix(sqlite-native): restore kv error hook#4635

Open
NathanFlurry wants to merge 1 commit into04-12-perf_sqlite-native_avoid_cloning_cached_read_chunksfrom
04-12-fix_sqlite-native_restore_kv_error_hook
Open

fix(sqlite-native): restore kv error hook#4635
NathanFlurry wants to merge 1 commit into04-12-perf_sqlite-native_avoid_cloning_cached_read_chunksfrom
04-12-fix_sqlite-native_restore_kv_error_hook

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 13, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude bot commented Apr 13, 2026

PR Review: fix(sqlite-native): restore kv error hook

This PR restores KV error hooks in the native SQLite implementation, adds a last_error tracking mechanism in the VFS layer, and surfaces meaningful error context to the JavaScript layer. Overall the approach is sound and the implementation is clean.


Correctness Fixes (Good)

notGlobal ?? false (wrapper.js): Genuine bug fix — previously passing undefined to the native binding which expected a boolean.

.as_deref() fix (vfs.rs:681): More type-correct than .as_ref() for getting a &[u8] slice from Option<Vec<u8>>. The previous .as_ref() would produce Option<&Vec<u8>> rather than Option<&[u8]>, which could cause type mismatches silently.

read_cache.as_ref().and_then(...) (vfs.rs:636): Handles read_cache correctly as an Option type. The type definition change driving this is not visible in the diff — if FileState::read_cache changed from a bare HashMap to Option<HashMap> in a parent branch, this fix is correct. Worth confirming that is the case.


Error Tracking Design

The Mutex<Option<String>> approach for last_error in VfsContext is correct for this use case. The split between clone_last_error() (used by SQLite’s kv_vfs_get_last_error) and take_last_error() (used by the JS layer) is a clean design: SQLite can call kv_vfs_get_last_error non-destructively, while the JS side consumes the error once.

The clear_last_error() on success pattern prevents stale errors from leaking across operations.


Potential Issues

Possible TOCTOU in error clearing: After a KV failure, report_kv_error sets last_error. SQLite receives SQLITE_IOERR and may call kv_vfs_get_last_error. However, SQLite may also retry or issue a follow-up VFS operation (e.g. a read on the journal file) that succeeds, triggering clear_last_error() before kv_vfs_get_last_error is called. This is unlikely in practice since SQLite does not retry after IO errors, but the window exists.

wrapNativeStorageError always throws but the call sites are not explicit about it:

try {
    const result = await nativeDb.query(query, bindings);
    return mapRows(result.rows, result.columns);
} catch (error) {
    wrapNativeStorageError(nativeDb, error); // always throws, but a reader cannot tell
}

A reader may wonder why there is no return or throw after the call. Restructuring call sites as throw wrapNativeStorageError(...) would make the intent clear and, if this code is ever compiled via TypeScript, allow never narrowing.

report_kv_error calls on_error after set_last_error: The error is stored before the hook fires. Not a bug, but future on_error implementations that inspect VFS state should be aware the stored error is already set at that point.


Minor Notes

  • The error message referencing c.abortSignal is helpful and directly actionable for actor developers.
  • sqlite_error_message() properly handles the null-db case, and SQLite docs confirm sqlite3_errmsg is safe to call on a non-null handle even after sqlite3_open_v2 fails.
  • kv_vfs_get_last_error now correctly implements the null-terminated string copy contract (bounds check + null terminator) where it was previously a no-op stub.
  • The on_error() default no-op in the SqliteKv trait is the right pattern for an optional hook.

Summary

The implementation is solid. The one structural suggestion is to make wrapNativeStorageError call sites explicit about the function always throwing (e.g. throw wrapNativeStorageError(...)). Everything else looks correct and follows codebase conventions.

@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from ceec0d4 to ff117f9 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 6eac78f to 4e380c8 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from ff117f9 to e25c1b6 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch 2 times, most recently from 7fbbf37 to fe8cf4f Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from e25c1b6 to 532364f Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry marked this pull request as ready for review April 14, 2026 21:32
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 2be63d0 to a0f54bb Compare April 14, 2026 23:43
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from 22df032 to 8793c33 Compare April 14, 2026 23:43
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from 8793c33 to b32bcb1 Compare April 21, 2026 00:43
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from a0f54bb to 416d461 Compare April 21, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant